<template>
<view>
  <view v-if="examineed">
    <view class="sticky"> 为了您的资金安全，请务必确认填写自己的身份信息 </view>
    <view class="boxs">
      <view class="imgs">
        <image @tap="touchphoto1" class="photo" :src="pic1"></image>
        <image @tap="touchphoto2" class="photo" :src="pic2"></image>
        <image @tap="touchphoto3" class="photo" :src="pic3"></image>
      </view>
      <view class="forms">
        <u-form
          :label-width="130"
          label-position="left"
          :model="form"
          ref="uForm"
        >
          <u-form-item label="真实姓名" prop="name">
            <u-input
              :disabled="realnameState"
              :border="border"
              placeholder="请输入您的真实姓名"
              v-model="form.name"
            />
          </u-form-item>
          <u-form-item label="身份证号" prop="IDcard">
            <u-input
              :disabled="realnameState"
              :border="border"
              placeholder="请输入您的身份证号码"
              v-model="form.IDcard"
            />
          </u-form-item>
        </u-form>
      </view>
      <!-- <view class="agreement">
				<u-checkbox v-model="check" @change="checkboxChange"></u-checkbox>
				<view class="agreement-text">
					已阅读并同意<text>《用户保密协议》</text>
				</view>
			</view> -->
      <view v-if="!realnameState" class="btnBox">
        <u-button
          ripple-bg-color="#5a8cdf"
          shape="circle"
          :ripple="true"
          class="btn"
          type="primary"
          @click="submit"
          >提交</u-button
        >
      </view>
    </view>
  </view>
  <view v-else style="padding: 150px 0; text-align: center;">
    开发中,敬请期待...
  </view>
</view>
</template>

<script>
import api from "../../../../api/routes.js";
export default {
  data() {
    return {
      examineed:false,
      realnameState: false,
      check: true,
      lisPic1:
        "https://recruit-oss.oss-cn-beijing.aliyuncs.com/recruit/img/two.png",
      lisPic2:
        "https://recruit-oss.oss-cn-beijing.aliyuncs.com/recruit/img/one.png",
      lisPic3:
        "https://recruit-oss.oss-cn-beijing.aliyuncs.com/recruit/img/three.png",
      pic1: "https://recruit-oss.oss-cn-beijing.aliyuncs.com/recruit/img/two.png",
      pic2: "https://recruit-oss.oss-cn-beijing.aliyuncs.com/recruit/img/one.png",
      pic3: "https://recruit-oss.oss-cn-beijing.aliyuncs.com/recruit/img/three.png",
      type: "textarea",
      border: false,
      height: 100,
      autoHeight: true,
      form: {
        name: "",
        IDcard: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入您的真实姓名",
            trigger: ["blur", "change"],
          },
        ],
        IDcard: [
          {
            required: true,
            message: "请输入您的身份证号码",
            trigger: ["change", "blur"],
          },
          {
            validator: (rule, value, callback) => {
              console.log();
              return this.checkIdCard(value);
            },
            message: "身份证号输入有误",
            // 触发器可以同时用blur和change
            trigger: ["change", "blur"],
          },
        ],
      },
    };
  },
  onLoad() {
    this.memberCertInfo();
    this.getconfig()
  },
  methods: {

    getconfig() {
		console.log(4444444);
      this.$myRequest({
        url: api.config,
        method: "GET",
        data: {
          key: "mini_audit_state_v1.0.0",
        },
      }).then((res) => {
        console.log(res.data.config,3332888888);
		this.examineed = res.data.config=='审核中'?false: true
      });
    },

    checkIdCard(num) {
      var idcard = num;
      if (idcard == "") {
        return true;
      }
      var regex1 =
        /^[1-9][0-7]\d{4}((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))\d{3}(\d|X|x)?$/;
      // 身份号码位数及格式检验
      switch (idcard.length) {
        case 15:
          if (
            (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 ||
            ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 &&
              (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0)
          ) {
            var regex2 =
              /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/; // 测试出生日期的合法性
          } else {
            var regex2 =
              /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/; // 测试出生日期的合法性
          }

          if (regex2.test(idcard)) {
            return true;
          } else {
            return false;
          }
        case 18:
          if (regex1.test(idcard)) {
            var S =
              (parseInt(idcard[0]) + parseInt(idcard[10])) * 7 +
              (parseInt(idcard[1]) + parseInt(idcard[11])) * 9 +
              (parseInt(idcard[2]) + parseInt(idcard[12])) * 10 +
              (parseInt(idcard[3]) + parseInt(idcard[13])) * 5 +
              (parseInt(idcard[4]) + parseInt(idcard[14])) * 8 +
              (parseInt(idcard[5]) + parseInt(idcard[15])) * 4 +
              (parseInt(idcard[6]) + parseInt(idcard[16])) * 2 +
              parseInt(idcard[7]) * 1 +
              parseInt(idcard[8]) * 6 +
              parseInt(idcard[9]) * 3;
            var Y = S % 11;
            var M = "F";
            var JYM = "10X98765432";
            M = JYM.substr(Y, 1);
            // 判断校验位
            if (M == idcard[17].toUpperCase()) {
              // alert(Errors[0]+"18");
              return true;
            } else {
              // alert(Errors[3]);
              // showErrMsg = Errors[3];
              return false;
            }
          } else {
            return false;
          }
        default:
          // alert(Errors[1]);
          // showErrMsg = Errors[1];
          return false;
      }
    },

    uploadImg(imgPath, type) {
      uni.uploadFile({
        url: "https://recruit-api.xujiekeji.com/recruit-server/app/file/upload", //post请求的地址
        filePath: imgPath,
        name: "file",
        success: (uploadFileRes) => {
          var obj = JSON.parse(uploadFileRes.data);
          if (type == 1) {
            this.pic1 = obj.url;
          } else if (type == 2) {
            this.pic2 = obj.url;
          } else {
            this.pic3 = obj.url;
          }
        },
      });
    },
    touchphoto1() {
      if (this.realnameState) {
        return;
      }
      uni.chooseImage({
        count: 1,
        sizeType: ["original", "compressed"],
        sourceType: ["camera", "album"], //这要注意，camera掉拍照，album是打开手机相册
        success: (res) => {
          console.log(res);

          const tempFilePaths = res.tempFilePaths;

          this.uploadImg(tempFilePaths[0], 1);
        },
      });
    },
    touchphoto2() {
      if (this.realnameState) {
        return;
      }
      uni.chooseImage({
        count: 1,
        sizeType: ["original", "compressed"],
        sourceType: ["camera", "album"], //这要注意，camera掉拍照，album是打开手机相册
        success: (res) => {
          console.log(res);

          const tempFilePaths = res.tempFilePaths;
          this.uploadImg(tempFilePaths[0], 2);
        },
      });
    },

    touchphoto3() {
      if (this.realnameState) {
        return;
      }
      uni.chooseImage({
        count: 1,
        sizeType: ["original", "compressed"],
        sourceType: ["camera"], //这要注意，camera掉拍照，album是打开手机相册
        success: (res) => {
          console.log(res);

          const tempFilePaths = res.tempFilePaths;
          this.uploadImg(tempFilePaths[0], 3);
        },
      });
    },

    // 勾选版权协议
    checkboxChange(e) {
      console.log(e);
      this.check = e.value;
    },

    async memberCertInfo() {
      const res = await this.$myRequest({
        url: api.memberCertInfo,
        method: "GET",
        data: {},
      });
      console.log(res);
      let { certBack, certFront, certNo, photo, realnameState, certName } =
        res.data.member;
      this.pic1 = certBack ? certBack : this.lisPic1;
      this.pic2 = certFront ? certFront : this.lisPic2;
      this.pic3 = photo ? photo : this.lisPic3;
      this.form.name = certName;
      this.form.IDcard = certNo;
      this.realnameState = realnameState == 0 ? true : false;
    },

    async memberCert() {
      const res = await this.$myRequest({
        url: api.memberCert,
        method: "POST",
        data: {
          certName: this.form.name,
          certNo: this.form.IDcard,
          certFront: this.pic1,
          certBack: this.pic2,
          photo: this.pic3,
        },
      });

      if (res.data.code == 0) {
        uni.showToast({
          title: res.data.msg,
          duration: 2000,
        });
        setTimeout(() => {
          uni.switchTab({
            url: "/pages/my/my",
          });
        }, 2000);
      }
    },
    submit() {
      this.$refs.uForm.validate((valid) => {
        if (valid) {
          console.log(this.pic1);
          console.log(this.lisPic1);

          if (this.pic1 == this.lisPic1) {
            uni.showToast({
              icon: "none",
              title: "请上传身份证正面",
            });
            return;
          }
          if (this.pic2 == this.lisPic2) {
            uni.showToast({
              icon: "none",
              title: "请上传身份证反面",
            });
            return;
          }

          if (this.pic3 == this.lisPic3) {
            uni.showToast({
              icon: "none",
              title: "请上传肖像照",
            });
            return;
          }
          // if(!this.check) return this.$u.toast('请勾选协议');

          console.log("验证通过");

          this.memberCert();
        } else {
          console.log("验证失败");
        }
      });
    },
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
};
</script>
<style>
page {
  background: rgb(245, 247, 249);
}
</style>
<style lang="scss" scoped>
.boxs {
  .forms {
    background: #ffffff;
    display: block;
    padding: 0 3%;
    margin: 20rpx 3% 0;
    border-radius: 3px;
  }

  .agreement {
    display: flex;
    align-items: center;
    margin: 25rpx 3%;

    .agreement-text {
      padding-left: 0rpx;
      color: $u-tips-color;
      text {
        color: $my-primary;
      }
    }
  }
}

.sticky {
  display: flex;
  align-items: center;
  background-color: #fffaf4;
  color: #f9aa45;
  padding: 15rpx 32rpx;
  font-size: 13px;
}

.btnBox {
  width: 94%;
  margin: 40rpx 3% 0;
}

.btn {
  background: $my-primary;
}

.imgs {
  padding: 32rpx 3% 0;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;

  .photo {
    width: 48%;
    height: 230rpx;
    margin-bottom: 10rpx;
  }
}
</style>
